package com.example.demo.mapper;
import com.example.demo.entity.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
    List<User_Test2> findAll();
    boolean register(String admin,String pswd);
    boolean exist(String admin);
    String getPassword(String admin);
    boolean createUserTable();
    //boolean createMessageTable();
    //改变策略，直接用一个大表装所有的聊天数据，用用户id做区分，按时间排序
    //聊天数据不做存储，由客户端存储，数据库只做数据传输
    User_Test2 getUserByName(String name);
    String modify_password(String account,String password);
//    @Select("SELECT BUSINESS FROM USER WHERE ACCOUNT = #{account};")
    String getBusinessByAccount(String account);

    @Select("SELECT id FROM USER WHERE ACCOUNT = #{account}")
    String getId(String account);
    @Insert("INSERT INTO USER (nickName,account,password,customer,business) VALUES (#{nickName},#{account},#{password},#{customer},#{business})")
    boolean register(String nickName,String account,String password,int customer,int business);
    @Select("SELECT NICKNAME FROM USER WHERE ACCOUNT = #{account}")
    String getNickNameByAccount(String account);
    @Select("SELECT CUSTOMER FROM USER WHERE ACCOUNT = #{account}")
    String getCustomerByAccount (String account);
    @Update("UPDATE USER SET CUSTOMER = 1 WHERE ACCOUNT = #{account}")
    boolean businessCreateCustomer(String account);
    //将用户更新为商家用户
    @Select("SELECT ID FROM USER WHERE ACCOUNT = #{admin} AND PASSWORD = #{password}")
    String getUserId(String admin,String password);
    @Insert("insert into goodsdetail (businessid,imageUrl,title,price,duration,specification,logoUrl,shopName,goodsGroup,goodsClass,discount,discount_price,storage,timestamp) values (#{businessId},#{imageUrl},#{title},#{price},#{duration},#{specification},#{logoUrl},#{shopName},#{goodsGroup},#{goodsClass},#{discount},#{discount_price},#{storage},#{timestamp})")
    boolean addNewGoods(GoodsDetail goodsDetail);
    @Select("SELECT BUSINESSID FROM GOODSDETAIL WHERE GOODSID = #{goodsId}")
    String getIdByGoods(int goodsId);
    @Insert("insert into Address (id,provinceId,cityId,areaId,fullname) values (#{id},#{provinceId},#{cityId},#{areaId},#{fullName})")
    boolean addNewAddress(Address address);
    @Select("SELECT * FROM GOODSDETAIL")
    GoodsDetail[] getAllGoodsDetail();
    @Select("SELECT * FROM USERDETAIL WHERE TITLE LIKE '%#{like}%'")
    GoodsDetail[] getGoodsDetailsLikeSomeThing(String like);
    @Insert("INSERT INTO LABELS (LABEL,GOODSID) VALUES (#(label.label),#(label.goodsId))")
    int addLabel(Label label);
    @Select("SELECT * FROM LABELS WHERE GOODSID = #{goodsId}")
    Label[] getLabelsByGoodsId(int goodsId);
    @Select("SELECT GOODSID FROM GOODSDETAIL WHERE TIMESTAMP = #{timestamp} AND BUSINESSID = #{businessId}")
    int getGoodsId (long timestamp,String businessId);
    @Select("SELECT SHAREID FROM SHARES WHERE TIMESTAMP = #{timestamp} AND ACCOUNT = #{account}")
    int getShareId (long timestamp,String account);
    @Insert("INSERT INTO SHARES (account,message,timestamp) VALUES (#{account},#{message},#{timestamp})")
    boolean addNewShare(Share share);
    @Select("SELECT * FROM SHARES")
    Share[] getAllShare();
    @Select("SELECT businessId FROM goodsDetail WHERE goodsId = #{goodsId}")
    String getAccountByGoodsId(String goodsId);
    @Select("SELECT * FROM goodsDetail where businessId = #{businessId}")
    GoodsDetail[] getDetailsByBusinessId(String businessId);
    @Select("SELECT shopName from user where account = #{businessId}")
    String getShopNameByBusinessId(String businessId);
    @Select("SELECT goodsId FROM goodsDetail WHERE businessId = #{businessId}")
    int[] getGoodsIdsByBusinessId(String businessId);
    @Update("UPDATE user SET shopName = #{shopName} WHERE account = #{account}")
    int setShopNameByAccount(String shopName,String account);
    @Select("SELECT * FROM goodsDetail WHERE goodsId = #{goodsId}")
    GoodsDetail getDetailById(String goodsId);
    @Select("SELECT * FROM goodsDetail WHERE goodsClass = #{goodsClass} AND goodsGroup = #{goodsGroup}")
    GoodsDetail[] getDetailsByClassAndGroup(int goodsClass,int goodsGroup);
    @Select("SELECT title FROM goodsDetail where goodsId = #{goodsId}")
    String getGoodsNameByGoodsId(String goodsId);
//    @Select("SELECT title FROM goodsDetail where goodsId = #{goodsId}")
//    String getGoodsNameById(int goodsId);
    //orders订单表中的操作
    @Insert("INSERT INTO orders (goodsId,num,userId,startTime,orderNo,orderState) VALUES (#{goodsId},#{num},#{userId},#{startTime},#{orderNo},#{orderState})")
    int addOrder(Order order);
    @Update("UPDATE orders SET orderState = #{orderState} WHERE orderNo = #{orderNo}")
    boolean updateOrderState(Order order);
    @Select("SELECT * FROM orders where userId = #{userId}")
    Order[] getOrdersByUserId(String userId);
    @Select("SELECT * FROM orders where goodsId = #{goodsId}")
    Order[] getOrdersByGoodsId(int goodsId);
    @Update("UPDATE goodsDetail SET title = #{title},price = #{price},specification = #{specification},duration = #{duration},goodsGroup = #{goodsGroup},goodsClass = #{goodsClass},discount = #{discount},discount_price = #{discount_price},storage = #{storage} WHERE goodsId = #{goodsId}")
    boolean updateGoodsDetail(GoodsDetail detail);
    //address地址表中的操作
    @Select("SELECT * FROM address WHERE account = #{account}")
    Address[] getAddressByAccount(String account);
    @Update("UPDATE address SET main = 0 WHERE account = #{account} AND main = 1")
    boolean modifyMainAddress(String account);
    @Update("UPDATE address SET main = 1 WHERE id = #{id}")
    boolean addMain(int id);
    @Insert("INSERT INTO address (provinceId,cityId,areaId,provinceName,cityName,areaName,fullName,account) values (#{provinceId},#{cityId},#{areaId},#{provinceName},#{cityName},#{areaName},#{fullName},#{account})")
    boolean addAddress(Address address);
}

